<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null,
      //   allData: [],// 服务器返回的数据
      allData: [
        {
          name: "商家1",
          value: 99
        },
        {
          name: "商家2",
          value: 102
        },
        {
          name: "商家3",
          value: 83
        },
        {
          name: "商家4",
          value: 49
        },
        {
          name: "商家5",
          value: 200
        },
        {
          name: "商家6",
          value: 152
        },
        {
          name: "商家7",
          value: 76
        },
        {
          name: "商家8",
          value: 23
        },
        {
          name: "商家9",
          value: 87
        },
        {
          name: "商家10",
          value: 223
        },
        {
          name: "商家11",
          value: 145
        },
        {
          name: "商家12",
          value: 187
        },
        {
          name: "商家13",
          value: 127
        },
        {
          name: "商家14",
          value: 57
        },
        {
          name: "商家15",
          value: 99
        }
      ],
      currentPage: 1, // 当前显示的页数
      totalPage: 0, // 一共有多少页
      timerId: null
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    // 屏幕适配
    window.addEventListener("resize", this.screenAdapter);
    // 页面首次加载完成的时候,主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    initChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E728F", "#23E5E5"],
        ["#5052EE", "#AB6EE5"]
      ];
      // 初始化chartInstance对象
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme);
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▍商家销售统计",
          // textStyle: {
          //     fontSize: 66
          // },
          left: 20,
          top: 12
        },
        grid: {
          top: "18%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true //距离是否包含坐标轴上的文字
        },
        xAxis: {
          type: "value"
        },
        yAxis: {
          type: "category"
          // data: sellerNames
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            z: 0,
            label: {
            backgroundColor: '#6a7985'
          },
            lineStyle: {
              color: "rgba(180, 180, 180, 0.2)",
              type: "solid"
            }
          },
        },
        series: [
          {
            type: "bar",
            // data: sellerValues,
            // barWidth: 66,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white"
              }
            },
            itemStyle: {
              barBorderRadius: [0, 33, 33, 0],
              color: arg => {
                let targetColorArr = null;
                if (arg.value > 180) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 100) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ]);
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(initOption);
      //   对图标进行鼠标事件监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      //   const { data: res } = await this.$http.get("seller");
      //   this.allData = res;
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大排序
      });
      //   每5个元素显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map(item => {
        return item.name;
      });
      const sellerValues = showData.map(item => {
        return item.value;
      });
      const dataOption = {
        yAxis: {
          data: sellerNames
        },
        series: {
          data: sellerValues
        }
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize * 1.2
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose();//销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    }
  }
};
</script>
<style scoped>

</style>
